
#include "ResManager.h"
#include "Sprite_Indics.h"
#include "spriteslist.h"

ResManager::ResManager()
{
	m_sprites		 = NULL;
	m_spritesState	 = NULL;
	m_spritesToLoad	 = NULL;
	m_nMaxSprites	 = 0;
	m_nStepLoadSprite = 0;
	
}

void ResManager::initialize()
{
	_initializeSprites();

}

void ResManager::_initializeSprites()
{
	m_nMaxSprites		= SPRITE_NUM;
	m_sprites			= GL_NEW ASprite[m_nMaxSprites]; 
	m_spritesState		= GL_NEW bool[m_nMaxSprites]; 
	m_spritesToLoad		= GL_NEW bool[m_nMaxSprites]; 
}

void ResManager::setSpriteToLoad(u32 id)
{
	if(!m_spritesState[id])
		m_spritesToLoad[id] = true;
}

void ResManager::_loadSprite(u32 id)
{
	char buffer[32];

	sprintf(buffer,"data/%s.ssp",kImageList[id]);
	m_sprites[id].LoadSprite(buffer);
	m_sprites[id].LoadImageW(kImageList[id]);

}

ASprite* ResManager::getSprite(u32 sprId)const { 
		return &m_sprites[sprId]; 
	};

bool ResManager::stepLoadRes()
{
	while(m_nStepLoadSprite< SPRITE_NUM)
	{
		if(m_spritesToLoad[m_nStepLoadSprite])
		{
			//if (!m_sprites[m_nStepLoadSprite].hasLoad())
			{
				_loadSprite(m_nStepLoadSprite);
			}
			m_spritesToLoad[m_nStepLoadSprite] = false;
			m_spritesState[m_nStepLoadSprite] = true;
			m_nStepLoadSprite++;
			return false;
		}
		m_nStepLoadSprite++;
	}

	return true;
	
}